package com.personal.controller;

import com.personal.pojo.Blogs;
import com.personal.pojo.BlogsCover;
import com.personal.pojo.Result;
import com.personal.server.BlogsServer;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blogs")
public class BlogsController {

    @Autowired
    private BlogsServer blogsServer;

    @PostMapping
    public Result insert(@RequestBody Blogs blogs) {
        log.info("添加新博客");
        boolean b = blogsServer.add(blogs);
        if(!b){
            return Result.error("添加失败");
        }
        return Result.success("添加成功");
    }


    @GetMapping("/{id}")
    public Result getBlogsByUserId(@PathVariable String id) {
        log.info("查询博客，用户id：{}",id);
        List<Blogs> b = blogsServer.getBlogsByUserId(id);
        return Result.success(b);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id) {
        log.info("删除博客");
        boolean b = blogsServer.delete(id);
        if(!b){
            return Result.error("删除失败");
        }
        return Result.success("删除成功");
    }

    @PutMapping
    public Result update(@RequestBody Blogs blogs) {
        log.info("更新博客{}",blogs.getId());
        boolean b = blogsServer.update(blogs);
        if(!b){
            return Result.error("更新失败");
        }
        return Result.success("更新成功");
    }

    @GetMapping("/all")
    public Result getAllBlogs() {
        log.info("查询所有博客");
        List<Blogs> b = blogsServer.getAllBlogs();
        return Result.success(b);
    }


    @GetMapping("/allcover")
    public Result getAllBlogsCover() {
        log.info("查询所有博客封面");
        List<BlogsCover> b = blogsServer.getAllBlogsCover();
        return Result.success(b);
    }

    @GetMapping("/mycover/{id}")
    public Result getMyBlogsCoverById(@PathVariable Integer id) {
        log.info("查询所有博客封面");
        List<BlogsCover> b = blogsServer.getMyBlogsCoverById(id);
        return Result.success(b);
    }


    @GetMapping("/page")
    public Result getPageBlogs(@RequestParam int page, @RequestParam int number) {
        log.info("查询博客，页码：{}，数量：{}",page,number);
        List<Blogs> b = blogsServer.getBlogsByTitle(page,number);
        return Result.success(b);
    }

    @GetMapping("/getOne/{id}")
    public Result getOneBlogsById(@PathVariable Integer id) {
        log.info("查询博客，id：{}",id);
        Blogs b = blogsServer.getOneBlogsById(id);
        if(b == null){
            return Result.error("查询失败");
        }
        return Result.success(b);
    }

    @GetMapping("/searchBlogs/{title}")
    public Result searchBlogsByTitle(@PathVariable String title) {
        log.info("查询博客，标题：{}",title);
        List<Blogs> b = blogsServer.searchBlogsByTitle(title);
        if(b == null){
            return Result.error("查询失败");
        }
        List<BlogsCover> bc = blogsServer.searchBlogsByBlogsId(b);
        return Result.success(bc);
    }



}
